import { compute } from '../libs/utils.js'

function calculatorReducer(data) {
    function setNumber(field, value) {

        if (typeof (value) !== 'number') {
            value = Number(value);
        }

        data[field] = value;
        return compute(
            data.curMethod,
            data.firstNumber,
            data.secondNumber
        );
    }

    function changeMethod(method) {
        data.curMethod = method;
        return compute(
            data.curMethod,
            data.firstNumber,
            data.secondNumber
        );
    }

    // 抛出一个闭包
    return {
        setNumber,
        changeMethod
    }
}

export default calculatorReducer;